<?php declare(strict_types=1);

/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\User\Ctl;

use Srv\Apps\Apis\Common\IdxCtl\IdxIp;
use Srv\Apps\Proto\DTO\User\DesktopRewardListDTO;
use Srv\Apps\Proto\VO\User\DesktopRewardListVO;
use Srv\Apps\Proto\VO\User\DesktopRewardVO;
use Srv\Apps\Proto\VO\User\UserInfoBasicVO;
use Srv\Libs\Common\CommArray;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Frame\Page;
use Srv\Libs\Storage\DataMysql;
use Srv\Libs\Tools\IpAddr;

final class DesktopRewardCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;     // DB-Write

    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
        }
        return $thisObject;
    }

    /**
     * @param Page $Page
     * @param array $whereData
     * @return DesktopRewardListVO|null
     * getDesktopRewardList
     */
    public function getDesktopRewardList(Page $Page, array $whereData):?DesktopRewardListVO
    {
        $limitString        = $Page->getLimitString();
        $whereString        = ' WHERE dtrr_delete=0 ';
        $whereInfoString    = ' WHERE usi_delete=0';
        $infoSearch         = false;
        // 检索条件
        if(count($whereData) > 0) foreach ($whereData as $key => $val){
            switch ($key){
                case DesktopRewardListDTO::_dtrrId:
                    $whereString            .= (strlen($whereString)>0?' AND ':'').'dtrr_id='. intval($val);
                    break;
                case DesktopRewardListDTO::_userId:
                    $whereString            .= (strlen($whereString)>0?' AND ':'').'user_id='. intval($val);
                    break;
                case DesktopRewardListDTO::_idNum:
                    if(strlen($val) > 10 && is_numeric($val)){
                        $whereString        .= (strlen($whereString)>0?' AND ':'').'user_id='. intval($val);
                    }else{
                        $infoSearch         = true;
                        $whereInfoString    .= (strlen($whereInfoString)>0?' AND ':'').'usi_idnum LIKE \'%'.$this->DbAuto->getRealEscapeString($val).'%\'';
                    }
                    break;
                case DesktopRewardListDTO::_createTimeStart:
                    $whereString        .= (strlen($whereString)>0?' AND ':'').'dtrr_create_time>='. intval($val);
                    break;
                case DesktopRewardListDTO::_createTimeStop:
                    $whereString        .= (strlen($whereString)>0?' AND ':'').'dtrr_create_time<='. intval($val);
                    break;
            }
        }
        if($infoSearch){
            $subSql         = 'SELECT user_id FROM '.$this->DbAuto->getTableNameAll('user_info').$whereInfoString;
            $whereString    .=  (strlen($whereString)>0?' AND ':'').'user_id IN('.$subSql.')';
        }
        $isFail             = false;
        $sql                = 'SELECT SQL_CALC_FOUND_ROWS dtrr_id, user_id, dup_id, dcht_id, dtrr_version, dtrr_ip, dtrr_reward_num, dtrr_reward_time, dtrr_create_time, dtrr_update_time FROM '.$this->DbAuto->getTableNameAll('desktop_reward_record').$whereString.' ORDER BY dtrr_id DESC '.$limitString;
        $list               = $this->DbAuto->getData($sql, $isFail);
        $totalNum           = $this->DbAuto->getDataInt('SELECT FOUND_ROWS() as num', 'num');
        if($isFail) return null;
        $DesktopRewardVOList        = [];
        if(CommArray::isForeach($list)){
            $UserCtl                = UserCtl::tag();
            $userIdList             = array_unique(array_filter(array_map('intval', array_column($list, 'user_id'))));
            $UserInfoBasicVOList    = $UserCtl->getUserInfoBasicBatch($userIdList);
            foreach($list as $item){
                $dtrrIdItem             = intval($item['dtrr_id']??0);
                if($dtrrIdItem < 1) continue;
                $userIdItem             = intval($item['user_id']);
                if($userIdItem < 1) continue;
                $DesktopRewardVO        = $this->getDesktopRewardVO($item);
                if(!($DesktopRewardVO instanceof DesktopRewardVO)) continue;
                $UserInfoBasicVO        = $UserInfoBasicVOList[$userIdItem]??null;
                if(!($UserInfoBasicVO instanceof UserInfoBasicVO)) continue;
                $DesktopRewardVO->setUserInfoBasic($UserInfoBasicVO);
                $DesktopRewardVOList[]  = $DesktopRewardVO;
            }
        }
        $DesktopRewardListVO            = new DesktopRewardListVO();
        $DesktopRewardListVO->setList($DesktopRewardVOList);
        $DesktopRewardListVO->setPage($Page->getPageVO($totalNum));
        return $DesktopRewardListVO;
    }

    /**
     * @param array $info
     * @return DesktopRewardVO|null
     * getDesktopRewardVO
     */
    public function getDesktopRewardVO(array $info):?DesktopRewardVO{
        $dtrrId             = intval($info['dtrr_id']??0);
        if($dtrrId < 1) return null;
        $DesktopRewardVO    = new DesktopRewardVO();
        $DesktopRewardVO->setDtrrId($dtrrId);
        $DesktopRewardVO->setUserId(intval($info['user_id']));
        $DesktopRewardVO->setDupId(intval($info['dup_id']));
        $DesktopRewardVO->setDchtId(intval($info['dcht_id']));
        $DesktopRewardVO->setVersion(trim($info['dtrr_version']));
        $ipString           = IdxIp::tag()->getIpStrByIntStr($info['dtrr_ip']);
        $ipArea             = IpAddr::getLocationInfo($ipString);
        $DesktopRewardVO->setIpStr($ipString.'('.($ipArea['country']??'').')');
        $DesktopRewardVO->setRewardNum(intval($info['dtrr_reward_num']));
        $DesktopRewardVO->setRewardTime(intval($info['dtrr_reward_time']));
        $DesktopRewardVO->setCreateTime(intval($info['dtrr_create_time']));
        $DesktopRewardVO->setUpdateTime(intval($info['dtrr_update_time']));
        return $DesktopRewardVO;
    }
}